#############################################
#        (c) Pavel Bacovsky, Ph.D.          #
#     			   Bates College		            #
#  	       Department of Politics		        #
#            Lewiston, ME 04240             #
#           pbacovsky@bates.edu             # 
#             www.bacovsky.phd              #
#############################################

### CONDITIONAL TREATMENT EFFECT PLOTS ###

install.packages("haven")
install.packages("interplot")
install.packages("patchwork")

library(haven)
library(interplot)
library(patchwork)

jeps_data <- read_dta("Bacovsky-LevelUpData_JEPS_Replication.dta") # Make sure to adjust your file paths!
View(jeps_data)

#DV1: General interest in US foreign trade policy, post-manip
#Importance of being a gamer
model1.1 <- lm(dv11_1~pt2*treatment+pt1, data=jeps_data)
summary(model1.1)
interplot1.1 <- interplot(m = model1.1, var1="treatment", var2 = "pt2") +
  # Add labels for X and Y axes
  ggtitle("Importance of being a gamer") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Not important at all", "2" = "Not very important",
                            "3" = "Very important", "4" = "Extremely important"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot1.1

#How well gamer describes
model1.2 <- lm(dv11_1~pt3*treatment+pt1, data=jeps_data)
summary(model1.2)
interplot1.2 <- interplot(m = model1.2, var1="treatment", var2 = "pt3") +
  # Add labels for X and Y axes
  ggtitle("How well gamer describes") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Not at all", "2" = "Not very well",
                              "3" = "Very well", "4" = "Extremely well"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot1.2

#Gamers = we
model1.3 <- lm(dv11_1~pt4*treatment+pt1, data=jeps_data)
summary(model1.3)
interplot1.3 <- interplot(m = model1.3, var1="treatment", var2 = "pt4") +
  # Add labels for X and Y axes
  ggtitle("Gamers = we") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Never", "2" = "Rarely",
                              "3" = "Some of the time", "4" = "Most of the time",
                              "5" = "All of the time"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold",),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot1.3

#Gamer self-image
model1.4 <- lm(dv11_1~pt5*treatment+pt1, data=jeps_data)
summary(model1.4)
interplot1.4 <- interplot(m = model1.4, var1="treatment", var2 = "pt5") +
  # Add labels for X and Y axes
  ggtitle("Gamer self-image") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Not at all", "2" = "Very little",
                              "3" = "Someewhat", "4" = "A great deal"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot1.4

#Gamer Identity Index
model1.5 <- lm(dv11_1~gid*treatment+pt1, data=jeps_data)
summary(model1.5)
interplot1.5 <- interplot(m = model1.5, var1="treatment", var2 = "gid") +
  # Add labels for X and Y axes
  ggtitle("Gamer Identity Index") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(breaks=seq(0,4,.5))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot1.5

#Time spent gaming, daily
model1.6 <- lm(dv11_1~pt9*treatment+pt1, data=jeps_data)
summary(model1.6)
interplot1.6 <- interplot(m = model1.6, var1="treatment", var2 = "pt9") +
  # Add labels for X and Y axes
  ggtitle("Time spend gaming, daily") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(breaks=seq(1,6,1),
                     labels=c("1" = "No time", "2" = "Up to 30 minutes", "3" = "30 minutes - 1 hours", "4" = "1-3 hours","5" = "3-6 hours", "6" = "More than 6 hours")) +
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot1.6

#Combining the graphs to create Figure A3.7
interplot1_complete <- interplot1.1 + interplot1.2 + interplot1.3 + interplot1.4+ interplot1.5 + interplot1.6
interplot1_complete

#DV2: Interest in the policy discussed in the prompt
#Importance of being a gamer
model2.1 <- lm(dv11_2~pt2*treatment, data=jeps_data)
summary(model2.1)
interplot2.1 <- interplot(m = model2.1, var1="treatment", var2 = "pt2") +
  # Add labels for X and Y axes
  ggtitle("Importance of being a gamer") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Not important at all", "2" = "Not very important",
                              "3" = "Very important", "4" = "Extremely important"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot2.1

#How well gamer describes
model2.2 <- lm(dv11_2~pt3*treatment, data=jeps_data)
summary(model2.2)
interplot2.2 <- interplot(m = model2.2, var1="treatment", var2 = "pt3") +
  # Add labels for X and Y axes
  ggtitle("How well gamer describes") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Not at all", "2" = "Not very well",
                              "3" = "Very well", "4" = "Extremely well"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot2.2

#Gamers = we
model2.3 <- lm(dv11_2~pt4*treatment, data=jeps_data)
summary(model2.3)
interplot2.3 <- interplot(m = model2.3, var1="treatment", var2 = "pt4") +
  # Add labels for X and Y axes
  ggtitle("Gamers = we") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Never", "2" = "Rarely",
                              "3" = "Some of the time", "4" = "Most of the time",
                              "5" = "All of the time"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold",),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot2.3

#Gamer self-image
model2.4 <- lm(dv11_2~pt5*treatment, data=jeps_data)
summary(model2.4)
interplot2.4 <- interplot(m = model2.4, var1="treatment", var2 = "pt5") +
  # Add labels for X and Y axes
  ggtitle("Gamer self-image") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(labels=c("1" = "Not at all", "2" = "Very little",
                              "3" = "Someewhat", "4" = "A great deal"))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot2.4

#Gamer Identity Index
model2.5 <- lm(dv11_2~gid*treatment, data=jeps_data)
summary(model2.5)
interplot2.5 <- interplot(m = model2.5, var1="treatment", var2 = "gid") +
  # Add labels for X and Y axes
  ggtitle("Gamer Identity Index") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(breaks=seq(0,4,.5))+
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot2.5

#Time spent gaming, daily
model2.6 <- lm(dv11_2~pt9*treatment, data=jeps_data)
summary(model2.6)
interplot2.6 <- interplot(m = model2.6, var1="treatment", var2 = "pt9") +
  # Add labels for X and Y axes
  ggtitle("Time spend gaming, daily") +
  ylab("Estimated conditional treatment effect") +
  scale_x_continuous(breaks=seq(1,6,1),
                     labels=c("1" = "No time", "2" = "Up to 30 minutes", "3" = "30 minutes - 1 hours", "4" = "1-3 hours","5" = "3-6 hours", "6" = "More than 6 hours")) +
  # Change the background
  theme_bw() +
  theme(plot.title = element_text(face="bold"),
        axis.text.x = element_text(size=12, angle=90),
        axis.text.y = element_text(size=12),
        axis.title = element_text(face= "bold", size=14)) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")
interplot2.6

#Combining the graphs to create Figure A3.8
interplot2_complete <- interplot2.1 + interplot2.2 + interplot2.3 + interplot2.4+ interplot2.5 + interplot2.6
interplot2_complete